package cm.NewsDaoImpl;

import cm.NewsDao.BaseDao;
import cm.NewsDao.NewsDao3;
import cm.entity.News3;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsDaoImpl3 extends BaseDao implements NewsDao3 {



    @Override
    public List<News3> getNewsList() {
        List<News3> newList=new ArrayList<News3>();
        try {
            //（3）获得Statement对象，执行SQL语句
            String sql="select * from news_comment where content like '军%'";
            Object[] params={};
            ResultSet rs=this.executeSQL(sql, params);
            //（4）处理执行结果(ResultSet)，
            while(rs.next()){
                int id=rs.getInt("id");
                int newsId=rs.getInt("newsId");

                String content=rs.getString("content");
                String author=rs.getString("author");
                String ip=rs.getString("ip");
                Date time=rs.getDate("createdate");

                //封装成新闻信息对象
                News3 news=new News3();
                news.setId(id);
                news.setNewsId(newsId);
                news.setContent(content);
                news.setAuthor(author);
                news.setIp(ip);
                news.setCreateDate(time);

                //将新闻对象放进集合中
                newList.add(news);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            //释放资源
            this.closeResource();
        }
        return newList;
    }


    @Override
    public boolean add(News3 news) {
        boolean flag=false;
        try {
            String sql="insert into news_comment(newsId,content,author,ip,createdate) values(?,?,?,?,?)";
            Object[] params={news.getNewsId(),news.getContent(), news.getAuthor(),news.getIp(),news.getCreateDate()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("插入新闻成功！");
            }
            flag=true;
        }finally{
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean delete(int id) {
        boolean flag=false;
        try {
            String sql = "delete from  news_comment where id=?";
            Object[] params={id};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("删除新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean update(News3 news) {
        boolean flag=false;
        try {
            String sql = "update News_comment set content=? where id=?";
            Object[] params={news.getContent(),news.getId()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("修改新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }
}
